<?php
   //irEntry.php
	 	error_reporting(E_PARSE | E_ERROR);
		//connect
		include('./includes/db_connect.php');
		//sanitize data passed from user
		include('../core_lib/core_functions.php');
		
		//get CIL locations for assignment box
		$query = "SELECT * FROM lst_location";
		//set locations to $result
		$location_result = mysql_query($query);
   
		//id there a CONSUMER ID passed?
		if($_GET['id']!= '0'){
				$title = 'Consumer Information & Referral';
				$id = sanitize($_GET['id']);
		} else {
				$title = 'Public Information & Referral';
				$id = 0;
		}
   
		if($_GET["record"]){  
      $stored_ir_entry = $_GET["record"];
      //echo "<h1>$stored_ir_entry</h1>";
      //run the query to get stored i&r info for record
      $stored_ir_query = "SELECT *
                         FROM irEntry
                         WHERE id = ".$stored_ir_entry.";";
      $result = mysql_query($stored_ir_query);
   
			if($row = mysql_fetch_array($result)) {
					$callerFirstName    = $row["callerFirstName"];
					$callerLastName     = $row["callerLastName"];
        $callerPhoneNumber  = $row["callerPhoneNumber"];
        $callerBirthDate    = $row["callerBirthDate"];
        $callerAddress      = $row["callerAddress"];        
        $callerCity         = $row["callerCity"];
        $callerState_id     = $row["callerState_id"];
        $callerZip          = $row["callerZip"];
        $callerCounty       = $row["callerCounty"];
        $date               = $row["date"]; //break this up
        $date_pieces        = explode("-", $date);
        $stored_dateYear    = $date_pieces[0];  //year
        $stored_dateMonth   = $date_pieces[1];  //month
        $stored_dateDay     = $date_pieces[2];  //day
        $irType             = $row["type_id"];
        $stored_minutes     = $row["minutes"];
        $stored_sent             = $row["sent"];
        $stored_sent_info        = $row["sent_info"];
        $stored_sent_agency      = $row["sent_agency"];
        $stored_sent_minutes     = $row["sent_minutes"];
        $stored_refer            = $row["refer"];
        $stored_refer_info       = $row["refer_info"];
        $stored_refer_agency     = $row["refer_agency"];
        $stored_refer_minutes    = $row["refer_minutes"];
        $stored_info             = $row["info"];
        $stored_info_info        = $row["info_info"];
        $stored_info_agency      = $row["info_agency"];
        $stored_info_minutes     = $row["info_minutes"];
        $stored_outcome_healthcare         = $row["outcome_healthcare"];
        $stored_outcome_transportation     = $row["outcome_transportation"];
        $stored_outcome_assistive_tech     = $row["outcome_assistive_tech"];
        $stored_notes          = $row["notes"];
        $stored_assignedTo     = $row["assignedTo"];
        $stored_entered_by     = $row["entered_by"];
        $stored_status         = $row["status"];
      }
   }
   if(false) {
   } else {
			$dateMonth = date("m", mktime(date("H"),date("i"),date("s"),date("m"), date("d"), date("Y")));
			$dateDay   = date("d", mktime(date("H"),date("i"),date("s"),date("m"), date("d"), date("Y")));
			$dateYear  = date("Y", mktime(date("H"),date("i"),date("s"),date("m"), date("d"), date("Y")));
			$citizen   = 1;
   }
?>
<form action="./actions/ProcessIrEntry.php" method="post">
   <div style="border:1px #ddd solid;padding:3px;">
      <h2><?php echo $title ?></h2>
<?php
     //ONLY SHOW THE CALLER INFO BOX IF THIS IS A PUBLIC I&R
    if($title == 'Public Information & Referral'){
?>
      <!-- caller info -->
      <div style="background:#eee; border:1px #ddd solid;padding:3px;">
         <table class="consumer_tab">
            <tr>
               <td>
                  <div class="formUnitSml">
                     <label>Caller First Name:</label>
                     <input type="text" name="callerFirstName" value="<?php echo $callerFirstName ?>"/>
                  </div>
               </td>
               <td>
                  <div class="formUnitSml">
                     <label>Caller Last Name:</label>
                     <input type="text" name="callerLastName" value="<?php echo $callerLastName ?>"/>
                  </div>
               </td>
            </tr>
            <tr>
               <td>
                  <div class="formUnitSml">
                     <label>Caller Phone Number:</label>
                     <input type="text" name="callerPhoneNumber" value="<?php echo $callerPhoneNumber ?>" style="width:150px"/>
                  </div>
               </td>
               <td>
                  <div class="formUnitSml">
                     <label>Caller Birth Date:</label>
                     <input type="text" name="callerBirthDate" value="<?php echo $callerBirthDate ?>" style="width:150px"/>
                  </div>
               </td>
            </tr>  
         </table>
         <table class="consumer_tab" width="100%" style="margin-top:5px;">
            <tr>
               <td colspan="3" style="padding-top:5px; border-top: 1px #ccc solid;">
                  <div class="formUnitSml" style="width:500px">
                     <label>Caller Address:</label>
                     <input type="text" name="callerAddress" value="<?php echo $callerAddress ?>" style="width:350px"/>
                  </div>
               </td>
            </tr>
            <tr>
               <td>
                  <div class="formUnitSml" style="width:240px">
                     <label style="width:50px">City:</label>
                     <input type="text" name="callerCity" style="width:150px" value="<?php echo $callerCity ?>"/>
                  </div>
               </td>
               <td>
                  <div class="formUnitSml" style="width:110px">
                     <label style="width:40px">State:</label>
                     <select name="callerState_id" style="width:50px">
<?php 
            $stateQuery = "SELECT * FROM lst_state";
            $result     = mysql_query($stateQuery);         
            
								while($row=mysql_fetch_array($result)) {    
										$state_id = $row["id"];
										$abbr     = $row["abbr"];          
              
										if($callerState_id==$state_id){           
											echo "<option value=\"".$state_id."\" selected>".$abbr."</option>";         
										}else{
											echo "<option value=\"".$state_id."\">".$abbr."</option>";
										}
            }
?>            
                   </select>
                </div>
               </td>
               <td>
                  <div class="formUnitSml" style="width:150px">
                     <label style="width:70px">Zip Code:</label>
                     <input type="text" name="callerZip" style="width:60px" value="<?php echo $callerZip ?>"/>
                  </div>
               </td>
            </tr>
            <tr>
               <td colspan="3" style="padding-top:5px; border-top: 1px #ccc solid;">
                  <div class="formUnitSml" style="width:500px">
                     <label>County:</label>
                     <input type="text" name="callerCounty" value="<?php echo $callerCounty ?>" style="width:350px"/>
                  </div>        
               </td>
            </tr>
         </table>
      </div>
      <br/>
      <!-- end caller info -->
<?php
    }//end of caller info block for public i&r
?>
      <!-- -->
      <table class="consumer_tab" id="irEditTbl">
         <tr>
             <td><h3>I&R Status: 
<?php   
      if (!$stored_ir_entry){      
						echo "New";              
      }elseif($stored_status == 1){
         echo "Open";
      }else{
         echo "Closed";
      } 
?>
			</h3></td>
        </tr>
        <tr>
         <!-- I&R DATE-->
            <td colspan="2">
               <div class="formUnitVertical">
               <label for="date">Date</label>
               <br/>
               <select name="dateMonth" id="dateMonth">
                       <option value="01">January</option>
                       <option value="02">February</option>
                       <option value="03">March</option>
                       <option value="04">April</option>
                       <option value="05">May</option>
                       <option value="06">June</option>
                       <option value="07">July</option>
                       <option value="08">August</option>
                       <option value="09">September</option>
                       <option value="10">October</option>
                       <option value="11">November</option>
                       <option value="12">December</option>
               </select>
               <!-- js -->             
               <script>					
					for(i=0; i < $('dateMonth').options.length; i++) {
							if($('dateMonth').options[i].value == '<? echo $stored_dateMonth ?>') {
								$('dateMonth').options[i].selected = true;
							}
	      }
	     </script>
               <select name="dateDay" id="dateDay">
<?php 
		    for($i=1; $i<32; $i++) {
						if($i<10){
	           echo "<option value=\"0".$i."\">".$i."</option>";
		      }else{
		          echo "<option value=\"".$i."\">".$i."</option>";
         } 
       }
?>
                </select>
                <script>					
						for(i=0; i<$('dateDay').options.length; i++) {
								if($('dateDay').options[i].value=='<? echo $stored_dateDay ?>') {
										$('dateDay').options[i].selected = true;
								}
						}
				</script>
                <select name="dateYear" id="dateYear">
<?php
		    for($i=0; $i<10; $i++) {
         $year = date("Y", mktime(0,0,0,1,1,date("Y")-$i));
						echo "<option value=\"".$year."\">".$year."</option>";
       }
?>
               </select>
                 <script>					
		     for(i=0; i<$('dateYear').options.length; i++) {
            if($('dateYear').options[i].value=='<? echo $stored_dateYear ?>') {
										$('dateYear').options[i].selected = true;
								}
		     }
				</script>
            </div>
            </td>
            <!-- TOTAL MINUTES SPENT ON I&R -->
            <td align="left" colspan="2">
      	       <div class="formUnitVertical" style="text-align:right">
                   <label for="date">Minutes</label>
                   <br/>
                   <input type="text" name="minutes" id="minutes" value="<?php echo $stored_minutes ?>" width="3" style="width:60px"/>
              </div>
             </td>
          </tr>
          <tr>
	     <td colspan="4"/>
          </tr>
          <!-- IR SUBJECT TYPE -->
          <tr>
    	     <td colspan="4">
                <div class="formUnit">
                   <label for="type" style="width:80px">Type:</label>
                   <select name="irType" style="width:500px">
<?php 
							$irQuery = "select * from lst_ir_type";
							$result  = mysql_query($irQuery);    
           while($row=mysql_fetch_array($result)) {             
									$type_id = $row["id"];
									$type = $row["type"];  
									if($irType==$type_id){    
               //ir type was already chosen in database
               echo "<option value=\"".$type_id."\" selected>".$type."</option>";       
             }else{                 
               echo "<option value=\"".$type_id."\">".$type."</option>";
             }
           }
?>
                  </select>
               </div>
            </td>
         </tr>
         <tr>
            <td colspan="4"/>
         </tr>
         <!-- I&R REFERRAL INFO -->
         <tr>
            <th></th>
            <th>Information</th>
            <th>Agency</th>
            <th>Minutes</th>
         </tr>
         <tr>
            <td>
               <div class="checkBoxUnit">
                  <input type="checkbox" name="sent" <?php if($stored_sent=="1") echo "checked";?>/>
                  <label for="sent">Sent </label>
				</div>
			</td>
			<td>
				<div class="formUnitVertical">          
					<input type="text" name="sentInfo" value="<?php echo $stored_sent_info ?>"/>
				</div>
			</td>
			<td>
					<div class="formUnitVertical">          
						<input type="text" name="sentAgency" value="<?php echo $stored_sent_agency; ?>"/>
					</div>
			</td>
			<td>
				<div class="formUnitVertical">
						<input type="text" name="sentMinutes" class="minute" value="<?php echo $stored_sent_minutes; ?>" width="3" style="width:60px"
											onkeyup="sumInputs('minute', 'irEditTbl', $('minutes'));"/>
				</div>
      </td>      
    </tr>
    <tr class="odd">
       <td>
          <div class="checkBoxUnit">
             <input type="checkbox" name="refer" <?php if($stored_refer=="1") echo "checked";?>/>
             <label for="refer">Refer</label>
          </div>
       </td>
       <td>
          <div class="formUnitVertical">          
             <input type="text" name="referInfo" value="<?php echo $stored_refer_info; ?>"/>
          </div>
       </td>
       <td>
          <div class="formUnitVertical">          
             <input type="text" name="referAgency" value="<?php echo $stored_refer_agency; ?>"/>
          </div>
      </td>
      <td>
      	 <div class="formUnitVertical">          
            <input type="text" name="referMinutes" class="minute" value="<?php echo $stored_refer_minutes; ?>" width="3" style="width:60px"
          	   onkeyup="sumInputs('minute', 'irEditTbl', $('minutes'));"/>
         </div>
      </td>
   </tr>
   <tr>
      <td>
         <div class="checkBoxUnit">
            <input type="checkbox" name="info" <?php if($stored_info=="1") echo "checked";?>/>
            <label for="info">Info </label>
        </div>
      </td>
      <td>
         <div class="formUnitVertical">          
            <input type="text" name="infoInfo" value="<?php echo $stored_info_info; ?>"/>
         </div>
      </td>
      <td>
         <div class="formUnitVertical">          
            <input type="text" name="infoAgency" value="<?php echo $stored_info_agency; ?>"/>
         </div>
      </td>
      <td>
         <div class="formUnitVertical">          
            <input type="text" name="infoMinutes" class="minute" value="<?php echo $stored_info_minutes; ?>" width="3" style="width:60px"
          	    onkeyup="sumInputs('minute', 'irEditTbl', $('minutes'));"/>
         </div>
      </td>
    </tr>
    <tr>
       <td colspan="4">
           <h3>Outcome Indicators</h3>
       </td>
    </tr>
    <tr>
       <td colspan="4">
         <table width="100%">
            <tr>
                  <!-- change -->
					<td>
						<div class="checkBoxUnit">
							<input type="checkbox" name="outcomeHealthcare" <?php if($stored_outcome_healthcare=="1") echo "checked";?>/>
							<label for="healthcare">Healthcare </label>
                   </div>
               </td>
               <td>
                  <div class="checkBoxUnit">
                     <input type="checkbox" name="outcomeTransportation" <?php if($stored_outcome_transportation=="1") echo "checked";?>/>
                    <label for="transportation">Transportation </label>
                  </div>
              </td>
              <td>
                 <div class="checkBoxUnit">
                    <input type="checkbox" name="outcomeAssistiveTech" <?php if($stored_outcome_assistive_tech=="1") echo "checked";?>/>
                    <label for="assistiveTech">Assistive Technology </label>     
                 </div>
             </td>
          </tr>
       </table>
     </td>
   </tr>
   <tr>
      <td valign="top" colspan="4">
         <label>Notes:</label>
         <br/><br/>
         <textarea name="notes" cols="115" rows="10">
            <?php echo $stored_notes ?>
         </textarea>
      </td>
    </tr>
    <tr>
<?php
   //ONLY SHOW THE ASSIGNMENT INFO BOX IF THIS IS A PUBLIC I&R
   if($title == 'Public Information & Referral'){     
?>
           <td colspan="2" align="left">
               <p>Assign I&R To:<br/>
                   <select name="assignmentForUser" style="width:275px">
                   <br/>
<?php
        //retrieve location data
	      while($row = mysql_fetch_array($location_result)) {
							$location   = $row["location"];
	         $id         = $row["id"];
          echo "<option value=\"\">--".$location."--</option>";
	         //get users from each respective location
          $userQuery  = "SELECT user_name 
                           FROM user 
                           WHERE location_id = ".$id;
          $userResult = mysql_query($userQuery);
          while($row2 = mysql_fetch_array($userResult)) {
								if($row2["user_name"] == $stored_assignedTo){
										echo "<option value=\"".$row2["user_name"]."\" selected>".$row2["user_name"]."</option>";
            }else{   
               //set each user as a select value option 
		            echo "<option value=\"".$row2["user_name"]."\">".$row2["user_name"]."</option>";
            }					
          }
          echo "<option value=\"\"></option>";
					}//end while
?>
                  </select>
               </p>
            </td>
            <td colspan="2" align="right">
      	         <input type="hidden" value="0" name="id" />
                 <input type="hidden" value="<?php echo $stored_ir_entry; ?>" name="stored_ir_entry" />
                 <input value="Submit" class="btn" type="submit">
                 <input class="btn" type="button" value="Cancel" onclick="window.location='admin.php'"/>
            </td>
<?php
    }else{
      //if($id) { 

?>
              <!--
				<p>	
                Upon completing this form, you will be required to 
                enter a Monthly ISR record for this consumer. <br/><br/>
              </p>
				-->
             <td colspan="4" align="right">
					<input type="hidden" value="<?php echo $id;?>" name="id" />
					<input value="Submit" class="btn" type="submit">
					<input class="btn" type="button" value="Cancel" onclick="window.location='admin.php?action=cieedit&id=<?php echo $id ?>'"/>
             </td>
<?php
   }
?>
      </tr>
    </table>
  </div>
</form>
<script>
	sumInputs('minute', 'irEntryTbl', $('minutes'));
</script>